package com.sy.olddriver.dao.impl;

import com.sy.olddriver.constant.AppConstant;
import com.sy.olddriver.dao.ProductDao;
import com.sy.olddriver.entity.Product;
import com.sy.olddriver.utils.DBUtils;
import com.sy.olddriver.vo.ProductQueryParam;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

public class ProductDaoImpl implements ProductDao {
    private QueryRunner runner = DBUtils.getRunner();

    @Override
    public List<Product> getNewestProducts() {
        try {
            return runner.query("select * from product order by created desc limit 0,?", new BeanListHandler<>(Product.class), AppConstant.NEWEST_SHOPPING_SHOW_COUNT);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> getHotestProducts() {
        try {
            return runner.query("select * from product order by clickcount desc limit 0,?", new BeanListHandler<>(Product.class), AppConstant.HOTEST_SHOPPING_SHOW_COUNT);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Long getProductsCountByCondition(ProductQueryParam param) {
        Integer cid = param.getCid();
        String q = param.getQ();
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) from product where 1=1 ");
        if (cid != null) {
            sql.append("and cid=" + cid);
        }
        if (StringUtils.isNotBlank(q)) {
            sql.append(" and title like '%" + q + "%'");
        }
        try {
            return runner.query(sql.toString(), new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1L;
    }

    @Override
    public List<Product> getProductsByCondition(ProductQueryParam param, Long start, Long pageSize) {
        Integer cid = param.getCid();
        String q = param.getQ();
        StringBuilder sql = new StringBuilder();
        sql.append("select * from product where 1=1 ");
        if (cid != null) {
            sql.append("and cid=" + cid);
        }
        if (StringUtils.isNotBlank(q)) {
            sql.append(" and title like '%" + q + "%'");
        }
        sql.append(" limit " + start + "," + pageSize + "");
        System.out.println(sql);
        try {
            return runner.query(sql.toString(), new BeanListHandler<>(Product.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Product getProductById(Integer pid) {
        try {
            return runner.query("select * from product where id=?", new BeanHandler<>(Product.class), pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> getProductsByPids(List<String> pids) {
        StringBuilder sql = new StringBuilder("select * from product where 1=1");
        if (CollectionUtils.isNotEmpty(pids)) {
            String pidStr = pids.stream().collect(Collectors.joining(","));
            sql.append(" and id in");
            sql.append("(");
            sql.append(pidStr);
            sql.append(") ");
            sql.append("ORDER BY FIELD(id," + pidStr + ")");
        }
        try {
            return runner.query(sql.toString(), new BeanListHandler<>(Product.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

}
